/*
*  Matrix3.cpp
*
*  Created on: Jan 14, 2012
*  
*/

#include  "Matrix3.h"
#include <math.h>

namespace Physics
{
	Matrix3::Matrix3()
	{
		xx = xy = xz = 0.0f;
		yx = yy = yz = 0.0f;
		zx = zy = zz = 0.0f;
	}

	Matrix3::Matrix3( float XX, float XY, float XZ, 
					  float YX, float YY, float YZ,
					  float ZX, float ZY, float ZZ )
	{
		xx = XX; xy = XY; xz = XZ;
		yx = YX; yy = YY; yz = YZ;
		zx = ZX; zy = ZY; zz = ZZ;
	}

	Matrix3::~Matrix3()
	{
		// do nothing for now
	}

	// figure out a new approach to this....might have to code a [] operator
	float Matrix3::getIndex(int row, int col)
	{
		float value = 0.0f;

		switch(row)
		{
		case 0:

			switch(col)
			{
				case 0:
					value = xx;
					break;

				case 1:
					value = xy;
					break;

				case 2:
					value = xz;
					break;

				default:
					value = 0.0f;
					break;
			}
			
			break;

		case 1:

			switch(col)
			{
				case 0:
					value = yx;
					break;

				case 1:
					value = yy;
					break;

				case 2:
					value = yz;
					break;

				default:
					value = 0.0f;
					break;

			}
			
			break;

		case 2:

			switch(col)
			{
				case 0:
					value = zx;
					break;

				case 1:
					value = zy;
					break;

				case 2:
					value = zz;
					break;

				default:
					value = 0.0f;
					break;

			}
			
			break;

		default:
			value = 0.0f;
			break;

		}

		return value;
	}

	void Matrix3::identity()
	{
		xx = yy = zz = 1.0f;
		xy = xz = yx = 0.0f;
		yz = zx = zy = 0.0f;
	}

	Matrix3 Matrix3::operator +(Matrix3 &matrix)
	{
		return Matrix3(xx + matrix.getIndex(0,0), xy + matrix.getIndex(0,1), xz + matrix.getIndex(0,2),
					   yx + matrix.getIndex(1,0), yy + matrix.getIndex(1,1), yz + matrix.getIndex(1,2),
					   zx + matrix.getIndex(2,0), zy + matrix.getIndex(2,1), zz + matrix.getIndex(3,2) );

	}

	Matrix3 Matrix3::operator -(Matrix3 &matrix)
	{
		return Matrix3(xx - matrix.getIndex(0,0), xy - matrix.getIndex(0,1), xz - matrix.getIndex(0,2),
					   yx - matrix.getIndex(1,0), yy - matrix.getIndex(1,1), yz - matrix.getIndex(1,2),
					   zx - matrix.getIndex(2,0), zy - matrix.getIndex(2,1), zz - matrix.getIndex(3,2) );
	}
	
	// Had an issue when programming it. Each calculation is correct,
	// but when making the matrix to return it it had issues
	Matrix3 Matrix3::operator *(Matrix3 &matrix)
	{	
		Matrix3 result( 
						(xx * matrix.getIndex(0,0)) + (xy * matrix.getIndex(1,0)) + (xz * matrix.getIndex(2,0)),
						(xx * matrix.getIndex(0,1)) + (xy * matrix.getIndex(1,1)) + (xz * matrix.getIndex(2,1)),
						(xx * matrix.getIndex(0,2)) + (xy * matrix.getIndex(1,2)) + (xz * matrix.getIndex(2,2)),
	
						(yx * matrix.getIndex(0,0)) + (yy * matrix.getIndex(1,0)) + (yz * matrix.getIndex(2,0)),
						(yx * matrix.getIndex(0,1)) + (yy * matrix.getIndex(1,1)) + (yz * matrix.getIndex(2,1)),
						(yx * matrix.getIndex(0,2)) + (yy * matrix.getIndex(1,2)) + (yz * matrix.getIndex(2,2)),
	
						(zx * matrix.getIndex(0,0)) + (zy * matrix.getIndex(1,0)) + (zz * matrix.getIndex(2,0)),
						(zx * matrix.getIndex(0,1)) + (zy * matrix.getIndex(1,1)) + (zz * matrix.getIndex(2,1)),
						(zx * matrix.getIndex(0,2)) + (zy * matrix.getIndex(1,2)) + (zz * matrix.getIndex(2,2))
						);

		return result;
	}
		 
	void Matrix3::Scale(float scaleX, float scaleY, float scaleZ)
	{
		xx *= scaleX; xy *= scaleX; xz *= scaleX;
		yx *= scaleY; yy *= scaleY; yz *= scaleY; 
		zx *= scaleZ; zy *= scaleZ; zz *= scaleZ;			
	}

	void Matrix3::Scale(float scaleAmount)
	{
		Scale(scaleAmount, scaleAmount, scaleAmount);
	}

	/* //not sure if these methods are needed in Matrix3
	void Matrix3::Translate(float translateX, float translateY, float translateZ)
	{
	
	}
	
	void Matrix3::RotateX(float theta)
	{
		
	}

	void Matrix3::RotateY(float theta)
	{

	}

	void Matrix3::RotateZ(float theta)
	{
		
	}
	*/
}